<?php
/**
 * Created by PhpStorm.
 * User: wuchao
 * Date: 2020/4/15
 * Time: 10:48
 */

namespace app\api\model;

use \Exception;
use think\facade\Db;
use \think\response\Json;

class CateGoryModel
{
    private static $_table = 'category'; // 分类表
    private static $goods = 'goods'; //
//    private static $_dirverTable = 'dirver'; // 司机表
//    private static $_dirver_routeTable = 'dirver_route'; // 司机路由表

    /**获取一级分类
     * @param array $input
     * @return Json
     */
    public static function GetCategory($input)
    {
        $data = Db::table(self::$_table)->where('admin_id', $input['admin_id'])->where('level', 0)->where('status', 1)->field('id,pic,name')->select()->toArray();
        return $data;
    }
    /**获取一级分类
     * @param array $input
     * @return Json
     */
    public static function OneCategory($input)
    {
        $data = Db::table(self::$_table)->where('admin_id', $input['admin_id'])->where('level', 0)->where('status', 1)->field('id,pic,name')->select()->toArray();
        return jsonSuccess('获取成功',$data);
    }
    /**获取二级分类
     * @param array $input
     * @return Json
     */
    public static function TwoCategory($input)
    {
        $data = Db::table(self::$_table)->where('admin_id', $input['admin_id'])->where('pid', $input['id'])->where('level', 1)->where('status', 1)->field('id,pic,name')->select()->toArray();
        return jsonSuccess('获取成功', $data);
    }

    /**获取三级分类和商品
     * @param array $input
     * @return Json
     */
    public static function ThreeCategory($input)
    {
        $field = 'a.id,a.pic,a.name,b.id as goods_id,b.name as goods_name,b.info as goods_info,b.image,b.price,b.ot_price,b.sales,b.stock';
        $data = Db::table(self::$_table)->alias('a')
            ->join(self::$goods . ' b', 'a.id = b.cate_id')
            ->where('a.admin_id', $input['admin_id'])
            ->where('a.pid', $input['id'])
            ->where('level', 2)
            ->where('a.status', 1)
            ->field($field)->select()->toArray();
        $NewData = self::GetRepeatValGroup($data, 'id');//TODO 组合数据
        return jsonSuccess('获取成功', $NewData);
    }

    /**组合数据，相同的分类为二维，商品为三维
     * @param array $input
     * @return Json
     */
    public static function GetRepeatValGroup($arr, $keys)
    {
        if (!is_array($arr) || !$keys) {
            return false;
        }
        $temp = array();
        foreach ($arr as $key => $val) {
            $temp[$val[$keys]]['id'] = $val['id'];
            $temp[$val[$keys]]['pic'] = $val['pic'];
            $temp[$val[$keys]]['name'] = $val['name'];
            $temp[$val[$keys]]['goods'][]= $val;
            foreach ($temp[$val[$keys]]['goods'] as $key=>&$value){
                unset($value['id']);
                unset($value['pic']);
                unset($value['name']);
            }
        }
        return $temp;
    }

    /**获取一级下所有三级分类
     * @param array $input
     * @return Json
     */
    public static function GetAllCategory($input)
    {
        $second = Db::table(self::$_table)->where('pid', $input['cate_id'])->field('id,pid')->select()->toArray();
        $second_ids = array_column($second, 'id');
        $three = Db::table(self::$_table)->where('pid', 'in', $second_ids)->field('id')->select()->toArray();
        return $three_ids = array_column($three, 'id');
    }

    //curl进行Https Get访问链接
    public static function curl_url($url)
    {
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $output = curl_exec($ch); //执行并获取HTML文档内容
        $data = json_decode($output);
        //释放curl句柄
        curl_close($ch);
        return $data;
    }
}